package com.xuln.common.category.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import com.xuln.common.category.dao.ICategoryDao;
import com.xuln.common.category.entity.Category;
import com.xuln.common.category.service.ICategoryService;
import com.xuln.framework.entity.PrimaryKey;
import com.xuln.framework.service.BaseService;

public class CategoryServiceImpl extends BaseService implements ICategoryService {
	@Resource
	ICategoryDao categoryDao;

	public List<Category> loadAll() {
		return categoryDao.loadAll();
	}

	public void delete(Long categoryId) {
		categoryDao.deleteById(categoryId);
	}

	public List<Category> getAllCategoryByType(String type, Locale locale) {
		return categoryDao.getAllCategoryByType(type, locale.toString());
	}

	public Category getCategory(Long categoryId, Locale locale) {
		return categoryDao.get(new PrimaryKey(categoryId, locale));
	}

	public List<Category> getChildCategory(Long categoryId, Locale locale) {
		return categoryDao.getChildCategory(categoryId, locale.toString());
	}

	public List<Category> getAllChildCategory(Long categoryId, Locale locale) {
		return categoryDao.getAllChildCategory(categoryId, locale.toString());
	}

	public List<Category> loadAll(Locale locale) {
		return categoryDao.loadAllByLocale(locale);
	}

	public void save(Category category) {
		Long id = categoryDao.getKey() + 1;
		category.getPK().setId(id);
		categoryDao.setNextKey(id);
		categoryDao.save(category);
	}

	public void update(Category category) {
		category.setModifyDate(new Date());
		try{
			
			categoryDao.update(category);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public List<Category> getAllParent(Long categoryId, Locale locale) {
		return categoryDao.getAllParent(categoryId, locale.toString());
	}
}
